package com.hue.springbootsecurity.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @Author: 姚凤桥
 * @Date: 2022/5/21 10:13
 * @Description: 身份认证方法, 实现SpringSecurity的UserDetailsService
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    //密码加密器
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String pwd = "123456";
        String encodePwd = passwordEncoder.encode(pwd);
        System.out.println("加密后的密码：" + encodePwd);

        //身份认证成功,指定账号、密码、权限
        return new User(username, encodePwd, AuthorityUtils.createAuthorityList("user-all"));
    }
}
